/*
 * Copyright 2012 Faculty of Informatics - Masaryk University.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.muni.fi.pa165.creatures.dao.impl;

import cz.muni.fi.pa165.creatures.dao.CreatureDAO;
import cz.muni.fi.pa165.creatures.dao.utils.DAOUtils;
import cz.muni.fi.pa165.creatures.model.Creature;
import cz.muni.fi.pa165.creatures.model.CreatureType;
import cz.muni.fi.pa165.creatures.model.Region;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * This class represents the implementation of all basic operations upon
 * {@link cz.muni.fi.pa165.creatures.model.Creature} instances.
 *
 * @author xmiklos1
 */
@Transactional
@Repository
public class CreatureDAOImpl implements CreatureDAO {

    @PersistenceContext
    private EntityManager em;

    @Override
    public Long create(Creature creature) {
        DAOUtils.notNull(creature);
        Region region;
        CreatureType creatureType;
        if (creature.getRegion() != null) {
            region = em.find(Region.class, creature.getRegion().getId());
            creature.setRegion(region);
        }
        if (creature.getCreatureType() != null) {
            creatureType = em.find(CreatureType.class, creature.getCreatureType().getId());
            creature.setCreatureType(creatureType);
        }

        em.persist(creature);
        return creature.getId();
    }

    @Override
    public void remove(Creature creature) {
        DAOUtils.notNull(creature);
        Creature toRemove = em.find(Creature.class, creature.getId());
        em.remove(toRemove);
    }

    @Override
    public void update(Creature creature) {
        DAOUtils.notNull(creature);
        em.merge(creature);
    }

    @Override
    public Creature get(Long id) {
        DAOUtils.notNull(id);
        return em.find(Creature.class, id);
    }

    @Override
    public Long getCount() {
        Query q = em.createQuery("select count(o) from Creature as o");
        return ((Long) q.getSingleResult());
    }

    @Override
    public List<Creature> getAll() {
        Query q = em.createQuery("from Creature");
        return q.getResultList();
    }

    @Override
    public List<Creature> getCreaturesOf(Region region) {
        DAOUtils.notNull(region);
        Query q = em.createQuery("select o from Creature as o "
                + "where o.region = :region");
        q.setParameter("region", region);
        return q.getResultList();
    }

    @Override
    public Long getCountOf(CreatureType creatureTypeEntity) {
        return Long.valueOf(this.getCreaturesOf(creatureTypeEntity).size());
    }

    @Override
    public Creature getByName(String name) {
        DAOUtils.notNull(name);
        Query q = em.createQuery("select o from Creature as o "
                + "where o.name = :name");
        q.setParameter("name", name);
        List result = q.getResultList();
        if (result.size() > 0) {
            return (Creature) result.get(0);
        } else {
            return null;
        }
    }

    @Override
    public List<Creature> getCreaturesOf(CreatureType creatureType) {
        DAOUtils.notNull(creatureType);
        Query q = em.createQuery("select o from Creature as o "
                + "where o.creatureType = :creatureType");
        q.setParameter("creatureType", creatureType);
        return q.getResultList();
    }

    @Override
    public void deleteAll() {
        Query q = em.createQuery("delete from Creature c");
        q.executeUpdate();
    }
}
